package hammer {
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.*;

	public class HammerButton extends Sprite {
		private var _text:String = "";
		private var _embedFonts:Boolean = true;
		private var _autoSize:String = TextFieldAutoSize.LEFT;
		private var _color:uint = 0xFFFFFF;
		private var _bgEnabled:Boolean = false;
		private var _bgColor:uint = 0x000000;
		private var _padding:uint = 4;
		private var _fontName:String = "";
		private var _fontSize:Object = null;
		private var _isHtml:Boolean = false;
		private var _tf:TextField;
		private var _bg:Shape;
		private var _tfOriginY:uint = 0;
		
		public function HammerButton(text:String="", 
				fontName:String="",
				fontSize:Object=null,
				color:uint=0xFFFFFF,
				bgEnabled:Boolean=false,
				bgColor:uint=0x000000,
				padding:uint=8,
				embedFonts:Boolean=true, 
				autoSize:String=TextFieldAutoSize.LEFT, 
				isHtml:Boolean=false
				) 
		{
			super();
			this._text = text;
			this._embedFonts = embedFonts;
			this._autoSize = autoSize;
			this._color = color;
			this._bgEnabled = bgEnabled;
			this._bgColor = bgColor;
			this._fontName = fontName;
			this._padding = padding;
			this._fontSize = fontSize;
			this._isHtml = isHtml;
			this.addEventListener(MouseEvent.MOUSE_OVER, _onMouseOver);
			this.addEventListener(MouseEvent.MOUSE_OUT, _onMouseOut);
			this.addEventListener(MouseEvent.CLICK, _onClick);
			
			// all 3 lines are needed to enable the hand cursor
			this.buttonMode = true;
			this.useHandCursor = true;
			this.mouseChildren = false;
			
			// add the text field and text
			layout();
		}
		
		public function get text():String {
			return _text;
		}
		
		public function set text(value:String):void {
			_text = value;
			layout();
		}
		
		private function layout():void {
			while (this.numChildren) {
				this.removeChildAt(0);
			}
			this.graphics.clear();
			
			_tf = new TextField();
			_tf.mouseEnabled = false;
			_tf.autoSize = _autoSize;
			_tf.wordWrap = false;
			_tf.selectable = false;
			_tf.textColor = _color;
			_tf.backgroundColor = _bgColor;
			_tf.background = _bgEnabled;
			_tf.antiAliasType = AntiAliasType.ADVANCED;
			if (_fontName != "") {
				var formatter:TextFormat = new TextFormat();
				formatter.font = _fontName;
				formatter.size = _fontSize;
				_tf.embedFonts = true;
				_tf.defaultTextFormat = formatter;	
			}
			if (_isHtml) {
				_tf.htmlText = _text;
			} else {
				_tf.text = _text;
			}
			this.addChild(_tf);
			_tf.x = _padding;
			_tf.y = _padding;
			_tfOriginY = _tf.y;
			
			_bg = new Shape();
			_bg.graphics.lineStyle();
			_bg.graphics.beginFill(0, 0);
			_bg.graphics.drawRect(0, 0, this.width + _padding, this.height + _padding);
			_bg.graphics.endFill();
			addChild(_bg);
			
		}
		
		private function _onMouseOver(e:MouseEvent):void {
			//trace("HammerButton._onMouseOver");
		}
		
		private function _onMouseOut(e:MouseEvent):void {
			//trace("HammerButton._onMouseOut");
		}
		
		private function _onClick(e:MouseEvent):void {
			//trace("HammerButton._onClick");
		}
	}
}